package com.y.lintcode;

import java.util.Arrays;

/**
 * @author Y2014188432
 * 统计数字
 *
 * 输入：
 *      k = 1, n = 12
 * 输出：
 *      5
 * 解释：
 *      在 [0,12] 中包含了 5 个 1
 */
public class Test2 {
    public static void main(String[] args) {
        long k = 7;
        long n = 5;
        test(k,n);
    }
    public static void test(long k,long n){
        long result = 0;
        String str = String.valueOf(n);
        String[] s = str.split("");
        int length = s.length;
        for (int i = 0; i < length; i++) {
            int si = Integer.parseInt(s[i]);

            // 当 k = 0 时，另行处理
            if (k == 0){
                // 0 不能位于首位
                if (i ==0){
                    if (length ==1){
                        result += 1;
                    }
                    continue;
                }
                // 处于末位时
                if (i == length-1){
                    result = result + Integer.parseInt(left(i,s,1)) + 1;
                } else {
                    if (k == si){
                        result = result + Integer.parseInt(left(i,s,1)+right(i,s,1)) + 1 - (int)Math.pow(10,length-1-i);
                    } else {
                        result = result + Integer.parseInt(left(i,s,1)+right(i,s,2)) + 1 - (int)Math.pow(10,length-1-i);
                    }
                }
                continue;
            }
            if (i == 0){
                if (k >si){ continue;}
                if (k ==si){
                    result = result + Integer.parseInt(right(i,s,1)) + 1;
                }else{
                    result = result + Integer.parseInt(right(i,s,2)) + 1;
                }
                continue;
            }
            if (i == length-1){
                if (k > si){
                    result = result + Integer.parseInt(left(i,s,2)) + 1;
                } else {
                    result = result + Integer.parseInt(left(i,s,1)) + 1;
                }
            } else {
                if (k == si){
                    result = result + Integer.parseInt(left(i,s,1)+right(i,s,1)) + 1;
                } else if (k >si){
                    result = result + Integer.parseInt(left(i,s,2)+right(i,s,2)) + 1;
                } else {
                    result = result + Integer.parseInt(left(i,s,1)+right(i,s,2)) + 1;
                }
            }
        }
        System.out.println(result);
    }

    public static String right(int i ,String[] s,int mark){
        if (mark ==1){
            String after1 = "";
            for (int j = i+1; j < s.length; j++) {
                after1 += s[j];
            }
            if (after1.equals("")){
                return "0";
            }
            return after1;
        }else{
            String after2 = "";
            for (int j = 0; j < s.length-1-i; j++) {
                after2 += "9";
            }
            if (after2.equals("")){
                return "0";
            }
            return after2;
        }
    }

    public static String left(int i , String[] s,int mark){
        String before = "";
        for (int j = 0; j < i; j++) {
            before += s[j];
        }
        if (mark == 1){
            return before;
        }else{
            return String.valueOf(Integer.parseInt(before)-1);
        }
    }
}
